﻿using System;
using System.Web.UI;
using DataAccess.Repository;

namespace KPI
{
	public partial class CreateKPI : BasePage
	{
		private IKPIRepository _KPIRepository;

		protected void Page_Init(object sender, EventArgs e)
		{
			if(!CanCreateKPI())
			{
				Response.Redirect("~/KPIList.aspx");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void cusvKPIContent_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			try
			{
				args.IsValid = (args.Value.Length <= 500);
			}
			catch
			{
				args.IsValid = false;
			}
		}

		protected void cusvActionPlan_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			try
			{
				args.IsValid = (args.Value.Length <= 500);
			}
			catch
			{
				args.IsValid = false;
			}
		}

		protected void btnSaveAndContinue_Click(object sender, EventArgs e)
		{
			if(Page.IsValid)
			{
				if(SaveKPI())
				{
					ClearInputValues();
					ShowMessage("添加成功,请继续添加", false);
				}
				else
				{
					ShowMessage("KPI添加失败了,请稍后再试或联系相关技术人员.", true);
				}
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			if(Page.IsValid)
			{
				if(SaveKPI())
				{
					var msg = TheCurrentLoginUser.DeptName + "新加了一条KPI, 请您审核；您可以点击下面链接进行审核:";
					CommonHelper.MessegeToAdmin(msg, KPISiteContext.Current.RequestURL + "KPIList.aspx?groupid=" + TheCurrentLoginUser.DeptID.ToString());

					Response.Redirect("~/KPIList.aspx?groupid=" + TheCurrentLoginUser.DeptID.ToString());
				}
				else
				{
					ShowMessage("KPI添加失败了,请稍后再试或联系相关技术人员.", true);
				}
			}
		}

		protected void btnCancel_Click(object sender, EventArgs e)
		{
			Response.Redirect("~/KPIList.aspx");
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(_KPIRepository != null)
			{
				((IDisposable)_KPIRepository).Dispose();
			}
		}

		private void ClearInputValues()
		{
			this.txtActionPlan.Text = string.Empty;
			this.txtKPIContent.Text = string.Empty;
			this.txtKPIFinishTime.Text = string.Empty;
			this.txtKPIResponsablePerson.Text = string.Empty;
			this.txtKPIResponseDept.Text = string.Empty;
			this.txtKPIResponsibleLeadership.Text = string.Empty;
			this.txtKPIRLTel.Text = string.Empty;
			this.txtKPIRPTel.Text = string.Empty;
			this.txtKPIStartTime.Text = string.Empty;
		}

		private bool SaveKPI()
		{
			var result = false;

			if(TheCurrentLoginUser != null && TheCurrentLoginUser.RoleName.Equals("unitadministrators"))
			{
				var theKPI = GetTheKPI();

				if(theKPI != null)
				{
					_KPIRepository = new KPIRepository();

					result = _KPIRepository.CreateKPI(theKPI);
				}
			}

			return result;
		}

		private DataAccess.Models.KPI GetTheKPI()
		{
			DataAccess.Models.KPI result = null;

			try
			{
				DataAccess.Models.KPI kpi = new DataAccess.Models.KPI();

				kpi.ActionPlans = txtActionPlan.Text.Trim();
				kpi.CurrentState = DataAccess.Models.KPIStates.Review_Pending;
				kpi.FinishTime = Convert.ToDateTime(txtKPIFinishTime.Text.Trim());
				kpi.GroupID = TheCurrentLoginUser.DeptID;
				kpi.KPIAddTime = DateTime.Now;
				kpi.KPIContent = txtKPIContent.Text.Trim();
				kpi.ResponsablePerson = txtKPIResponsablePerson.Text.Trim();
				kpi.ResponseDepartment = txtKPIResponseDept.Text.Trim();
				kpi.ResponsibleLeadership = txtKPIResponsibleLeadership.Text.Trim();
				kpi.RLTel = txtKPIRLTel.Text.Trim();
				kpi.RPTel = txtKPIRPTel.Text.Trim();
				kpi.StartTime = Convert.ToDateTime(txtKPIStartTime.Text.Trim());
				kpi.TheYear = DateTime.Now.Year;
				kpi.CreateUserName = TheCurrentLoginUser.UserName;

				result = kpi;
			}
			catch(FormatException)
			{
				ShowMessage("遇到错误,请检查日期格式是否正确", true);
			}

			return result;
		}

		/// <summary>
		/// 检查是否有添加KPI的权限
		/// </summary>
		/// <returns></returns>
		private bool CanCreateKPI()
		{
			var result = false;

			if(TheCurrentLoginUser != null)
			{
				if(!string.IsNullOrWhiteSpace(TheCurrentLoginUser.RoleName) && TheCurrentLoginUser.RoleName.Equals("unitadministrators"))
				{
					result = true;
				}
			}

			return result;
		}
	}
}